﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Photon.SocketServer;
using WorldOfSuperpowers.Cache;
using WorldOfSuperpowers_COMMON;
using WorldOfSuperpowers_COMMON.DataModel;

namespace WorldOfSuperpowers.Handler
{
    public class RegistHandler:BaseHandler
    {
        public override void InitCode()
        {
            code = OperationCode.Regist;
        }

        public override void TranslateDic(Dictionary<byte, object> message, MasterClientPeer peer, SendParameters sendParameters)
        {
            MasterServer.log.Debug("RegistHandler解析");
            object UserData = GetValueFromDic(code, message);
            if (UserData != null)
            {
                OperationResponse op = new OperationResponse();
                op.OperationCode = (byte)code;
                //todo 提取到了有效注册信息,在缓存中查找是否可用注册
                LoginModel Lmodel = ProtoTranslater.GetInstance().GetTypeFromBytes<LoginModel>(UserData);
                UserDataModel Umodel = new UserDataModel();
                Umodel.UserAccount = Lmodel.username;
                Umodel.UserPassword = Lmodel.password;
                Umodel.SkillArr = new ushort[50];
                //主动技能101-130，被动201-215，辅助301-305
                for (ushort i = 101; i <= 130; i++)
                {
                    Umodel.SkillArr[i - 101] = i;
                }
                for (ushort i = 201; i <= 215; i++)
                {
                    Umodel.SkillArr[i - 171] = i;
                }
                for (ushort i = 301; i <= 305; i++)
                {
                    Umodel.SkillArr[i - 256] = i;
                }
                if (UserCache.RegistNewUser(ref Umodel))
                {
                    //todo 注册账号成功，可以用此账号登陆
                    op.ReturnCode = (short)ReturnCode.Success;
                    message[((byte) code)] = ProtoTranslater.GetInstance().GetBytesFromType(Umodel);
                    op.Parameters = message;
                }
                else
                {
                    //todo 注册失败，需要重新注册
                    op.ReturnCode = (short)ReturnCode.Fail;
                    op.DebugMessage = "该账号已存在，请重新注册";
                }
                //将角色信息UserDataModel发送回去
                peer.SendOperationResponse(op, sendParameters);
            }
            else
            {
                MasterServer.log.Error("无法获取该消息内容");
            }
        }
    }
}
